home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / guile-docs / user / scm.info-4 < prev    next >
Encoding:
GNU Info File  |  1995-06-08  |  48.8 KB  |  1,408 lines

  1. This is Info file scm.info, produced by Makeinfo-1.55 from the input
  2. file scm.texi.
  3.  
  4. 
  5. File: scm.info,  Node: Gwish,  Next: System Calls,  Prev: Tk Facilities,  Up: Guile Facilities
  6.  
  7. Gwish
  8. =====
  9.  
  10.   Gwish is a Wish-like application of Guile, based on STk by Erick
  11. Gallesio.
  12.  
  13. Running Gwish
  14. -------------
  15.  
  16. Mapping Tcl/Tk Constructs onto Gwish
  17. ------------------------------------
  18.  
  19.   In Gwish, there is a single Tcl/Tk interpreter which is an implicit
  20. default.  Commands defined in that interpreter are automaticly defined
  21. as Scheme commands as well (unless a Scheme-specific binding overwrites
  22. them).  This means that you can write Scheme code that closely matches
  23. the corresponding Tcl code.  For example:
  24.  
  25.      (button '.b)
  26.      (.b 'configure :text "hello world" :command (tcl-lambda () (beep)))
  27.  
  28.   Arguments passed to a procedure implemented by a Tcl command must be
  29. converted to a string for the sake of Tcl's calling conventions and data
  30. representations.  This table illustrates how argument types are
  31. converted:
  32.  
  33.        ;;----------------------------------------
  34.        ;; Argument             Converted argument
  35.        ;;----------------------------------------
  36.          123                     "123"
  37.          123.34                  "123.34"
  38.          "a-string"              "a-string"
  39.          'a-symbol               "a-symbol"
  40.          :keyword                "-keyword"
  41.          #t                      "1"
  42.          #f                      "0"
  43.          (lambda (...) ...)      "*__guile#234"
  44.  
  45.   Most of these conversions are simple and obvious but the conversion of
  46. procedure objects is subtle and non-obvious.  It is not necessary to
  47. understand the details, though they are documented elsewhere.  It is
  48. necessary to know that procedure arguments `do the right thing' when
  49. they are passed as command parameters to widget configuration commands,
  50. and as event bindings.  You might ask: what is the right thing?
  51.  
  52.   A procedure passed as either an event binding or widget command is
  53. protected by the binding or widget involved.  Effectively, the widget
  54. or binding keeps a reference to the procedure object.  For this reason,
  55. it is possible to use an anonymous Scheme procedure as an event binding
  56. or widget command.  For example:
  57.  
  58.      (let ((n 0))
  59.         (.b configure :command (lambda ()
  60.                                   (write n)
  61.                                   (newline)
  62.                                   (set! n + 1)
  63.                                   "")))
  64.      
  65.      ;; Incidently, note that the command procedure returns an empty string.
  66.      ;; All procedures called by Tcl have to return a proper Tcl result.
  67.      ;; Return values are explained further in the next subsection.
  68.      ;;
  69.  
  70.   When a command or binding is overwritten and that command or binding
  71. is to a Scheme procedure, the reference to the procedure is dropped.
  72. The procedure is also dropped if the binding or widget is deleted.  For
  73. this reason, there are no core leaks associated with passing anonymous
  74. procedures as these kinds of arguments.
  75.  
  76.   At this time, it isn't generally useful to pass Scheme procedures to
  77. Tcl commands other than as an argument to a binding command or widget
  78. configuration command.
  79.  
  80.   The details of how procedures are translated are described in the
  81. documentation for `tcl-apply-command'.  *Note Tcl Facilities::.
  82.  
  83. Defining Tcl Command in Scheme
  84. ------------------------------
  85.  
  86.   Gwish provides a convenient syntax for defining new Tcl commands.
  87.  
  88.  - Syntax: proc NAME FORMALS . BODY
  89.      Define a new Tcl command and Scheme procedure simultaneously.
  90.  
  91.           ;; A two argument string-append callable as a Tcl command.
  92.           ;;
  93.           (proc string-smash (s1 s2)  (string-append s1 s2))
  94.  
  95.      Proc expands to a `tcl-lambda' form, which is explained below.
  96.      The example above is equivalent to:
  97.  
  98.           (begin
  99.             (define string-smash (tcl-lambda (s1 s2) (string-append s1 s2)))
  100.             (tcl-create-command the-interpreter 'string-smash string-smash))
  101.  
  102.  
  103.  - Syntax: tcl-lambda (?CALLING-CONVENTION? ?.? FORMALS) . BODY
  104.      Similar to lambda, but imposes Tcl-specific calling conventions.
  105.      Type declarations are supported.  This is best explained via
  106.      examples.
  107.  
  108.      A simple use of `tcl-lambda':
  109.  
  110.           ;; By default, when called from Tcl, all of the arguments
  111.           ;; to a tcl-lambda are strings.
  112.           ;;
  113.           (tcl-lambda (a b) (string-append a b))
  114.  
  115.      The return value of a tcl-lambda has special signficance when the
  116.      function is invoked by Tcl.
  117.  
  118.      An integer return value is converted to a string.  A string or
  119.      symbol is used used directly as a Tcl result.  A cons pair with an
  120.      integer car and string cdr is a Tcl status/message combination.
  121.  
  122.      Type declarations can be used to specify that parameters passed as
  123.      strings should be coerced to some other type.  For example:
  124.  
  125.           (define tcl-plus
  126.              (tcl-lambda ((number a) (number b))
  127.                 (+ a b)))
  128.           
  129.           (tcl-plus "23" 19)  => 42
  130.  
  131.      Tcl-lambda is very useful for using anonymous Scheme functions as
  132.      Tk event bindings.  For this purpose, you can can specify a Tk
  133.      "calling convention" by writing a string in the first of the
  134.      argument specification.  For example:
  135.  
  136.           (.c 'bind 'node "<Button-2>"
  137.               (tcl-lambda ("%x %y" (number x) (number y))
  138.                 (set! cur-x x)
  139.                 (set! cur-y y)
  140.                 #t))                    ; Tcl-compatible return value.
  141.  
  142.      In that example, `"%x %y"' is the calling convention.  The full
  143.      command that Tcl calls when invoking the binding will be similar
  144.      to:
  145.  
  146.           __binding#0x123123 %x %y
  147.  
  148.      and therefore the Scheme procedure will be passed (in this case)
  149.      the x and y position of the mouse.
  150.  
  151. An Example: Ousterhout's Graph Editor
  152. -------------------------------------
  153.  
  154.   This program implements the toy graph editor from the Tcl/Tk <fixme>.
  155.  
  156.  
  157. 
  158. File: scm.info,  Node: System Calls,  Next: Gscsh,  Prev: Gwish,  Up: Guile Facilities
  159.  
  160. System Calls
  161. ============
  162.  
  163.   A volunteer is need to write this section.  Most of the system call
  164. documentation already exists in other sections. See also *Note
  165. I/O-Extensions::, *Note Posix Extensions::.
  166.  
  167.   All of the unix system calls are available as Scheme procedures.
  168.  
  169.   Any call that fails with errno EINTR is automaticly retried.
  170.  
  171.   By default, any other error during a system call will cause the call
  172. to signal an error and not return.  There are two functions that control
  173. this behavior and which you can redefine.  These are `syscall' and
  174. `syserror'.  The default definitions are:
  175.  
  176.      (define (syserror key fn err . args)
  177.        (errno err)
  178.        (apply error (cons fn args)))
  179.      
  180.      (define (syscall thunk)
  181.        (errno 0)
  182.        (catch 'syserror thunk syserror))
  183.  
  184.   A typical system call is written this way:
  185.  
  186.      (define (connect port address . args)
  187.        (syscall
  188.         (lambda ()
  189.           (let ((rv (apply %connect (cons port (cons address args)))))
  190.             (if (eq? rv #f)
  191.             (throw 'syserror 'connect (errno) port address args))))))
  192.  
  193. 
  194. File: scm.info,  Node: Gscsh,  Prev: System Calls,  Up: Guile Facilities
  195.  
  196. Gscsh
  197. =====
  198.  
  199.  - Function: fork
  200.  - Function: fork THUNK
  201.      Fork a child process: returns child pid to parent, #f to child.
  202.  
  203.      With an argument, fork a child process executing the Scheme
  204.      procedure thunk.  Returns child pid to parent.
  205.  
  206.  - Function: wait
  207.  - Function: wait PID
  208.  - Function: wait PID OPTIONS
  209.      Wait for any child to terminate.
  210.  
  211.      Pid argument means for that child process to terminate.  Special
  212.      values for pid are: -1 wait for any child process; 0 wait for any
  213.      child process in the process group of the calling process.
  214.  
  215.      Options is an integer with the following OR'd values:
  216.           WNOHANG: do not wait.
  217.           WUNTRACED: also return status information from stopped processes.
  218.  
  219.  - Function: exec-pipe PIPE
  220.  - Function: fg-pipe PIPE
  221.  - Function: bg-pipe PIPE
  222.      These functions run external programs in a pipeline.  exec-pipe
  223.      causes the pipeline to overlay the current process.  fg-pipe waits
  224.      for the first command in the pipeline to exit and returns its exit
  225.      status code.  bg-pipe runs the pipeline in the background,
  226.      returning a list with the process id of the first program and
  227.      ports which can be used for communicating with the programs in the
  228.      pipeline.
  229.  
  230.      The pipe argument consists of a series of commands and optionally
  231.      connection lists.  A command can be either an external Unix
  232.      program or a scheme procedure.  An external program is written as
  233.      a list of strings with the name and arguments.  An example of a
  234.      pipeline connecting three programs and using the default
  235.      connection lists is:
  236.  
  237.           (exec-pipe
  238.             '("cat" "/etc/passwd") '("sort" "-t:" "+2") '("grep" "^u"))
  239.  
  240.      Alternatively a Scheme procedure (taking no arguments) may be
  241.      specified and will be run in its own subprocess.  The standard
  242.      ports (current-input-port, current-output-port and
  243.      current-error-port) are created automatically from file
  244.      descriptors 0, 1 and 2.  A Scheme procedure used in a pipeline
  245.      would typically be a filter, such as:
  246.  
  247.           (define filter
  248.             (lambda ()
  249.               (let loop ((ch (read-char)))
  250.                 (cond ((not (eof-object? ch))
  251.                    (display ch)
  252.                    (loop (read-char)))))))
  253.  
  254.      and the term in the pipeline would be written:
  255.  
  256.           `(,filter)
  257.  
  258.      A connection list specifies how open Scheme ports are connected to
  259.      Unix file descriptors in the programs in the pipeline and how file
  260.      descriptors are wired together between programs.  For example, the
  261.      default connection list between the sort and grep commands in the
  262.      pipeline above is:
  263.  
  264.           `((1 0) (2 ,(current-error-port)))
  265.  
  266.      which says to connect file descriptor 1 (standard output) from the
  267.      sort command to file descriptor 0 (standard input) in the grep
  268.      command and to connect file descriptor 2 (standard error) from the
  269.      sort command to the Scheme current error port.
  270.  
  271.      Connection lists may be placed between programs in the pipeline
  272.      and if omitted the default above will be used.  Connection lists
  273.      may also be placed before the first program and after the last to
  274.      specify connections at the beginning and end of the pipeline.  The
  275.      default connection list then depends on which pipeline function is
  276.      used.
  277.  
  278.      The defaults for exec-pipe and fg-pipe are:
  279.  
  280.           `((,(current-input-port) 0))
  281.           `((1 ,(current-output-port)) (2 ,(current-error-port)))
  282.  
  283.      for the input and output ends of the pipeline respectively.
  284.  
  285.      The defaults for bg-pipe are:
  286.  
  287.           '((return-port 0))
  288.           `((1 return-port) (2 ,(current-error-port)))
  289.  
  290.      The symbol 'return-port in the connection list adds a port to the
  291.      list returned by bg-pipe.  In the default case two ports are
  292.      returned: the first is an output port connected to the standard
  293.      input of the first program in the pipeline and the second is an
  294.      input port connected to the standard output of the last process.
  295.      [need to beware of deadlocks if multiple output ports are
  296.      returned].
  297.  
  298.      The pipeline example above can be rewritten with all the
  299.      connection lists given explicitly:
  300.  
  301.           (exec-pipe `((,(current-input-port) 0))
  302.             '("cat" "/etc/passwd") `((1 0) (2 ,(current-error-port)))
  303.             '("sort" "-t:" "+2")   `((1 0) (2 ,(current-error-port)))
  304.             '("grep" "^u") `((1 ,(current-output-port)) (2 ,(current-error-port)))
  305.  
  306.      Multiple file descriptors can be connected to a single target if a
  307.      connection list such as '((1 0) (2 0)) or equivalently '((1 2 0))
  308.      is specified.
  309.  
  310.      A final construction which can be used in a connection list is a
  311.      string to be interpreted as the name of a file.  For example, the
  312.      connection list between the sort and grep commands could be
  313.      written:
  314.  
  315.           '((1 0) (2 "sort-errors"))
  316.  
  317.      which would capture the standard error from sort to a newly
  318.      created file.  [Maybe this should be extended to allow appending
  319.      to a file, e.g., with ">sort-error-log", although it could also be
  320.      done with ports].
  321.  
  322. 
  323. File: scm.info,  Node: Internals,  Next: Procedure and Macro Index,  Prev: Guile Facilities,  Up: Top
  324.  
  325. Internals
  326. *********
  327.  
  328. * Menu:
  329.  
  330. * Data Types::
  331. * Operations::
  332. * Improvements To Make::
  333. * Finishing Dynamic Linking::
  334.  
  335. 
  336. File: scm.info,  Node: Data Types,  Next: Operations,  Prev: Internals,  Up: Internals
  337.  
  338. Data Types
  339. ==========
  340.  
  341. In the descriptions below it is assumed that `long int's are 32 bits in
  342. length.  Acutally, SCM is written to work with any `long int' size
  343. larger than 31 bits.  With some modification, SCM could work with word
  344. sizes as small as 24 bits.
  345.  
  346. All SCM objects are represented by type "SCM".  Type `SCM' come in 2
  347. basic flavors, Immediates and Cells:
  348.  
  349. * Menu:
  350.  
  351. * Immediates::
  352. * Cells::
  353. * Data Type Representations::
  354.  
  355. 
  356. File: scm.info,  Node: Immediates,  Next: Cells,  Prev: Data Types,  Up: Data Types
  357.  
  358. Immediates
  359. ----------
  360.  
  361. An "immediate" is a data type contained in type `SCM' (`long int').
  362. The type codes distinguishing immediate types from each other vary in
  363. length, but reside in the low order bits.
  364.  
  365.  - Macro: IMP X
  366.  - Macro: NIMP X
  367.      Return non-zero if the `SCM' object X is an immediate or
  368.      non-immediate type, respectively.
  369.  
  370.  - Immediate: inum
  371.      immediate 30 bit signed integer.  An INUM is flagged by a `1' in
  372.      the second to low order bit position.  The high order 30 bits are
  373.      used for the integer's value.
  374.  
  375.       - Macro: INUMP X
  376.       - Macro: NINUMP X
  377.           Return non-zero if the `SCM' X is an immediate integer or not
  378.           an immediate integer, respectively.
  379.  
  380.       - Macro: INUM X
  381.           Returns the C `long integer' corresponding to `SCM' X.
  382.  
  383.       - Macro: MAKINUM X
  384.           Returns the `SCM' inum corresponding to C `long integer' x.
  385.  
  386.       - Immediate Constant: INUM0
  387.           is equivalent to `MAKINUM(0)'.
  388.  
  389.      Computations on INUMs are performed by converting the arguments to
  390.      C integers (by a shift), operating on the integers, and converting
  391.      the result to an inum.  The result is checked for overflow by
  392.      converting back to integer and checking the reverse operation.
  393.  
  394.      The shifts used for conversion need to be signed shifts.  If the C
  395.      implementation does not support signed right shift this fact is
  396.      detected in a #if statement in `scmfig.h' and a signed right shift,
  397.      `SRS', is constructed in terms of unsigned right shift.
  398.  
  399.  - Immediate: ichr
  400.      characters.
  401.  
  402.       - Macro: ICHRP X
  403.           Return non-zero if the `SCM' object X is a character.
  404.  
  405.       - Macro: ICHR X
  406.           Returns corresponding `unsigned char'.
  407.  
  408.       - Macro: MAKICHR X
  409.           Given `char' X, returns `SCM' character.
  410.  
  411.  
  412.  - Immediate: iflags
  413.      These are frequently used immediate constants.
  414.  
  415.       - Immediate Constant: SCM BOOL_T
  416.           `#t'
  417.  
  418.       - Immediate Constant: SCM BOOL_F
  419.           `#f'
  420.  
  421.       - Immediate Constant: SCM EOL
  422.           `()'.  If `SICP' is `#define'd, `EOL' is `#define'd to be
  423.           identical with `BOOL_F'.  In this case, both print as `#f'.
  424.  
  425.       - Immediate Constant: SCM EOF_VAL
  426.           end of file token, `#<eof>'.
  427.  
  428.       - Immediate Constant: SCM UNDEFINED
  429.           `#<undefined>' used for variables which have not been defined
  430.           and absent optional arguments.
  431.  
  432.       - Immediate Constant: SCM UNSPECIFIED
  433.           `#<unspecified>' is returned for those procedures whose return
  434.           values are not specified.
  435.  
  436.  
  437.  - Macro: IFLAGP N
  438.      Returns non-zero if N is an ispcsym, isym or iflag.
  439.  
  440.  - Macro: ISYMP N
  441.      Returns non-zero if N is an ispcsym or isym.
  442.  
  443.  - Macro: ISYMNUM N
  444.      Given ispcsym, isym, or iflag N, returns its index in the C array
  445.      `isymnames[]'.
  446.  
  447.  - Macro: ISYMCHARS N
  448.      Given ispcsym, isym, or iflag N, returns its `char *'
  449.      representation (from `isymnames[]').
  450.  
  451.  - Macro: MAKSPCSYM N
  452.      Returns `SCM' ispcsym N.
  453.  
  454.  - Macro: MAKISYM N
  455.      Returns `SCM' iisym N.
  456.  
  457.  - Macro: MAKIFLAG N
  458.      Returns `SCM' iflag N.
  459.  
  460.  - Variable: isymnames
  461.      An array of strings containing the external representations of all
  462.      the ispcsym, isym, and iflag immediates.  Defined in `repl.c'.
  463.  
  464.  - Constant: NUM_ISPCSYM
  465.  - Constant: NUM_ISYMS
  466.      The number of ispcsyms and ispcsyms+isyms, respectively.  Defined
  467.      in `scm.h'.
  468.  
  469.  - Immediate: isym
  470.      `and', `begin', `case', `cond', `define', `do', `if', `lambda',
  471.      `let', `let*', `letrec', `or', `quote', `set!', `#f', `#t',
  472.      `#<undefined>', `#<eof>', `()', and `#<unspecified>'.
  473.  
  474.  - CAR Immediate: ispcsym
  475.      special symbols: syntax-checked versions of first 14 isyms
  476.  
  477.  - CAR Immediate: iloc
  478.      indexes to a variable's location in environment
  479.  
  480.  - CAR Immediate: gloc
  481.      pointer to a symbol's value cell
  482.  
  483.  - Immediate: CELLPTR
  484.      pointer to a cell (not really an immediate type, but here for
  485.      completeness).  Since cells are always 8 byte aligned, a pointer
  486.      to a cell has the low order 3 bits `0'.
  487.  
  488.      There is one exception to this rule, *CAR Immediate*s, described
  489.      next.
  490.  
  491. A "CAR Immediate" is an Immediate point which can only occur in the
  492. `CAR's of evaluated code (as a result of `ceval''s memoization process).
  493.  
  494. 
  495. File: scm.info,  Node: Cells,  Next: Data Type Representations,  Prev: Immediates,  Up: Data Types
  496.  
  497. Cells
  498. -----
  499.  
  500. "Cell"s represent all SCM objects other than immediates.  A cell has a
  501. `CAR' and a `CDR'.  Low-order bits in `CAR' identify the type of
  502. object.  The rest of `CAR' and `CDR' hold object data.  The number
  503. after `tc' specifies how many bits are in the type code.  For instance,
  504. `tc7' indicates that the type code is 7 bits.
  505.  
  506.  - Macro: NEWCELL X
  507.      Allocates a new cell and stores a pointer to it in `SCM' local
  508.      variable X.
  509.  
  510.      Care needs to be taken that stores into the new cell pointed to by
  511.      X do not create an inconsistent object.  *Note Signals::.
  512.  
  513. All of the C macros decribed in this section assume that their argument
  514. is of type `SCM' and points to a cell (`CELLPTR').
  515.  
  516.  - Macro: CAR X
  517.  - Macro: CDR X
  518.      Returns the `car' and `cdr' of cell X, respectively.
  519.  
  520.  - Macro: TYP3 X
  521.  - Macro: TYP7 X
  522.  - Macro: TYP16 X
  523.      Returns the 3, 7, and 16 bit type code of a cell.
  524.  
  525.  - Cell: tc3_cons
  526.      scheme cons-cell returned by (cons arg1 arg2).
  527.  
  528.       - Macro: CONSP X
  529.       - Macro: NCONSP X
  530.           Returns non-zero if X is a `tc3_cons' or isn't, respectively.
  531.  
  532.  - Cell: tc3_closure
  533.      applicable object returned by (lambda (args) ...).  `tc3_closure's
  534.      have a pointer to other the body of the procedure in the `CAR' and
  535.      a pointer to the environment in the `CDR'.
  536.  
  537.       - Macro: CLOSUREP X
  538.           Returns non-zero if X is a `tc3_closure'.
  539.  
  540.       - Macro: CODE X
  541.       - Macro: ENV X
  542.           Returns the code body or environment of closure X,
  543.           respectively.
  544.  
  545.  
  546. "Header"s are Cells whose `CDR's point elsewhere in memory, such as to
  547. memory allocated by `malloc'.
  548.  
  549.  - Header: spare
  550.      spare `tc7' type code
  551.  
  552.  - Header: tc7_vector
  553.      scheme vector.
  554.  
  555.       - Macro: VECTORP X
  556.       - Macro: NVECTORP X
  557.           Returns non-zero if X is a `tc7_vector' or if not,
  558.           respectively.
  559.  
  560.       - Macro: VELTS X
  561.       - Macro: LENGTH X
  562.           Returns the C array of `SCM's holding the elements of vector
  563.           X or its length, respectively.
  564.  
  565.  - Header: tc7_ssymbol
  566.      static scheme symbol (part of initial system)
  567.  
  568.  - Header: tc7_msymbol
  569.      `malloc'ed scheme symbol (can be GCed)
  570.  
  571.       - Macro: SYMBOLP X
  572.           Returns non-zero if X is a `tc7_ssymbol' or `tc7_msymbol'.
  573.  
  574.       - Macro: CHARS X
  575.       - Macro: UCHARS X
  576.       - Macro: LENGTH X
  577.           Returns the C array of `char's or as `unsigned char's holding
  578.           the elements of symbol X or its length, respectively.
  579.  
  580.  - Header: tc7_string
  581.      scheme string
  582.  
  583.       - Macro: STRINGP X
  584.       - Macro: NSTRINGP X
  585.           Returns non-zero if X is a `tc7_string' or isn't,
  586.           respectively.
  587.  
  588.       - Macro: CHARS X
  589.       - Macro: UCHARS X
  590.       - Macro: LENGTH X
  591.           Returns the C array of `char's or as `unsigned char's holding
  592.           the elements of string X or its length, respectively.
  593.  
  594.  - Header: tc7_bvect
  595.      uniform vector of booleans (bit-vector)
  596.  
  597.  - Header: tc7_ivect
  598.      uniform vector of integers
  599.  
  600.  - Header: tc7_uvect
  601.      uniform vector of non-negative integers
  602.  
  603.  - Header: tc7_fvect
  604.      uniform vector of short inexact real numbers
  605.  
  606.  - Header: tc7_dvect
  607.      uniform vector of double precision inexact real numbers
  608.  
  609.  - Header: tc7_cvect
  610.      uniform vector of double precision inexact complex numbers
  611.  
  612.  - Header: tc7_contin
  613.      applicable object produced by call-with-current-continuation
  614.  
  615.  - Header: tc7_cclo
  616.      Subr and environment for compiled closure
  617.  
  618.      A cclo is similar to a vector (and is GCed like one), but can be
  619.      applied as a function:
  620.  
  621.        1. the cclo itself is consed onto the head of the argument list
  622.  
  623.        2. the first element of the cclo is applied to that list.  Cclo
  624.           invocation is currently not tail recursive when given 2 or
  625.           more arguments.
  626.  
  627.       - Function: makcclo PROC LEN
  628.           makes a closure from the *subr* PROC with LEN-1 extra
  629.           locations for `SCM' data.  Elements of a CCLO are referenced
  630.           using `VELTS(cclo)[n]' just as for vectors.
  631.  
  632. A "Subr" is a header whose `CDR' points to a C code procedure.  Scheme
  633. primitive procedures are subrs.  Except for the arithmetic `tc7_cxr's,
  634. the C code procedures will be passed arguments (and return results) of
  635. type `SCM'.
  636.  
  637.  - Subr: tc7_asubr
  638.      associative C function of 2 arguments.  Examples are `+', `-',
  639.      `*', `/', `max', and `min'.
  640.  
  641.  - Subr: tc7_subr_0
  642.      C function of no arguments.
  643.  
  644.  - Subr: tc7_subr_1
  645.      C function of one argument.
  646.  
  647.  - Subr: tc7_cxr
  648.      These subrs are handled specially.  If inexact numbers are
  649.      enabled, the `CDR' should be a function which takes and returns
  650.      type `double'.  Conversions are handled in the interpreter.
  651.  
  652.      `floor', `ceiling', `truncate', `round', `$sqrt', `$abs', `$exp',
  653.      `$log', `$sin', `$cos', `$tan', `$asin', `$acos', `$atan',
  654.      `$sinh', `$cosh', `$tanh', `$asinh', `$acosh', `$atanh', and
  655.      `exact->inexact' are defined this way.
  656.  
  657.      If the `CDR' is `0' (`NULL'), the name string of the procedure is
  658.      used to control traversal of its list structure argument.
  659.  
  660.      `car', `cdr', `caar', `cadr', `cdar', `cddr', `caaar', `caadr',
  661.      `cadar', `caddr', `cdaar', `cdadr', `cddar', `cdddr', `caaaar',
  662.      `caaadr', `caadar', `caaddr', `cadaar', `cadadr', `caddar',
  663.      `cadddr', `cdaaar', `cdaadr', `cdadar', `cdaddr', `cddaar',
  664.      `cddadr', `cdddar', and `cddddr' are defined this way.
  665.  
  666.  - Subr: tc7_subr_3
  667.      C function of 3 arguments.
  668.  
  669.  - Subr: tc7_subr_2
  670.      C function of 2 arguments.
  671.  
  672.  - Subr: tc7_rpsubr
  673.      transitive relational predicate C function of 2 arguments.  The C
  674.      function should return either `BOOL_T' or `BOOL_F'.
  675.  
  676.  - Subr: tc7_subr_1o
  677.      C function of one optional argument.  If the optional argument is
  678.      not present, `UNDEFINED' is passed in its place.
  679.  
  680.  - Subr: tc7_subr_2o
  681.      C function of 1 required and 1 optional argument.  If the optional
  682.      argument is not present, `UNDEFINED' is passed in its place.
  683.  
  684.  - Subr: tc7_lsubr_2
  685.      C function of 2 arguments and a list of (rest of) `SCM' arguments.
  686.  
  687.  - Subr: tc7_lsubr
  688.      C function of list of `SCM' arguments.
  689.  
  690. A "ptob" is a port object, capable of delivering or accepting
  691. characters.  *Note Ports: (r4rs)Ports.  Unlike the types described so
  692. far, new varieties of ptobs can be defined dynamically (*note Defining
  693. Ptobs::.).  These are the initial ptobs:
  694.  
  695.  - ptob: tc16_inport
  696.      input port.
  697.  
  698.  - ptob: tc16_outport
  699.      output port.
  700.  
  701.  - ptob: tc16_ioport
  702.      input-output port.
  703.  
  704.  - ptob: tc16_inpipe
  705.      input pipe created by `popen()'.
  706.  
  707.  - ptob: tc16_outpipe
  708.      output pipe created by `popen()'.
  709.  
  710.  - ptob: tc16_strport
  711.      String port created by `cwos()' or `cwis()'.
  712.  
  713.  - ptob: tc16_sfport
  714.      Software (virtual) port created by `mksfpt()' (*note Soft
  715.      Ports::.).
  716.  
  717.  - Macro: PORTP X
  718.  - Macro: OPPORTP X
  719.  - Macro: OPINPORTP X
  720.  - Macro: OPOUTPORTP X
  721.  - Macro: INPORTP X
  722.  - Macro: OUTPORTP X
  723.      Returns non-zero if X is a port, open port, open input-port, open
  724.      output-port, input-port, or output-port, respectively.
  725.  
  726.  - Macro: OPENP X
  727.  - Macro: CLOSEDP X
  728.      Returns non-zero if port X is open or closed, respectively.
  729.  
  730.  - Macro: STREAM X
  731.      Returns the `FILE *' stream for port X.
  732.  
  733. Ports which are particularly well behaved are called "fport"s.
  734. Advanced operations like `file-position' and `reopen-file' only work
  735. for fports.
  736.  
  737.  - Macro: FPORTP X
  738.  - Macro: OPFPORTP X
  739.  - Macro: OPINFPORTP X
  740.  - Macro: OPOUTFPORTP X
  741.      Returns non-zero if X is a port, open port, open input-port, or
  742.      open output-port, respectively.
  743.  
  744. A "smob" is a miscellaneous datatype.  The type code and GCMARK bit
  745. occupy the lower order 16 bits of the `CAR' half of the cell.  The rest
  746. of the `CAR' can be used for sub-type or other information.  The `CDR'
  747. contains data of size long and is often a pointer to allocated memory.
  748.  
  749. Like ptobs, new varieties of smobs can be defined dynamically (*note
  750. Defining Smobs::.).  These are the initial smobs:
  751.  
  752.  - smob: tc_free_cell
  753.      unused cell on the freelist.
  754.  
  755.  - smob: tc16_flo
  756.      single-precision float.
  757.  
  758.      Inexact number data types are subtypes of type `tc16_flo'.  If the
  759.      sub-type is:
  760.  
  761.        0. a single precision float is contained in the `CDR'.
  762.  
  763.        1. `CDR' is a pointer to a `malloc'ed double.
  764.  
  765.        3. `CDR' is a pointer to a `malloc'ed pair of doubles.
  766.  
  767.       - smob: tc_dblr
  768.           double-precision float.
  769.  
  770.       - smob: tc_dblc
  771.           double-precision complex.
  772.  
  773.  - smob: tc16_bigpos
  774.  - smob: tc16_bigneg
  775.      positive and negative bignums, respectively.
  776.  
  777.      Scm has large precision integers called bignums.  They are stored
  778.      in sign-magnitude form with the sign occuring in the type code of
  779.      the SMOBs bigpos and bigneg.  The magnitude is stored as a
  780.      `malloc'ed array of type `BIGDIG' which must be an unsigned
  781.      integral type with size smaller than `long'.  `BIGRAD' is the
  782.      radix associated with `BIGDIG'.
  783.  
  784.  - smob: tc16_promise
  785.      made by DELAY.  *Note Control features: (r4rs)Control features.
  786.  
  787.  - smob: tc16_arbiter
  788.      synchronization object.  *Note Process Synchronization::.
  789.  
  790.  - smob: tc16_macro
  791.      macro expanding function.  *Note Low Level Syntactic Hooks::.
  792.  
  793.  - smob: tc16_array
  794.      multi-dimensional array.  *Note Arrays::.
  795.  
  796.      This type implements both conventional arrays (those with
  797.      arbitrary data as elements *note Conventional Arrays::.) and
  798.      uniform arrays (those with elements of a uniform type *note
  799.      Uniform Array::.).
  800.  
  801.      Conventional Arrays have a pointer to a vector for their `CDR'.
  802.      Uniform Arrays have a pointer to a Uniform Vector type (string,
  803.      bvect, ivect, uvect, fvect, dvect, or cvect) in their `CDR'.
  804.  
  805. 
  806. File: scm.info,  Node: Data Type Representations,  Prev: Cells,  Up: Data Types
  807.  
  808. Data Type Representations
  809. -------------------------
  810.  
  811. IMMEDIATE:      B,D,E,F=data bit, C=flag code, P=pointer address bit
  812.         ................................
  813. inum    BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB10
  814. ichr    BBBBBBBBBBBBBBBBBBBBBBBB11110100
  815. iflag                   CCCCCCC101110100
  816. isym                    CCCCCCC001110100
  817.         IMCAR:  only in car of evaluated code, cdr has cell's GC bit
  818. ispcsym                 000CCCC00CCCC100
  819. iloc    0DDDDDDDDDDDDDDDEFFFFFFF11111100
  820. pointer PPPPPPPPPPPPPPPPPPPPPPPPPPPPP000
  821. gloc    PPPPPPPPPPPPPPPPPPPPPPPPPPPPP001
  822.  
  823.    HEAP CELL:   G=gc_mark; 1 during mark, 0 other times.
  824.         1s and 0s here indicate type.     G missing means sys (not GC'd)
  825.         SIMPLE:
  826. cons    ..........SCM car..............0  ...........SCM cdr.............G
  827. closure ..........SCM code...........011  ...........SCM env.............G
  828.         HEADERs:
  829. ssymbol .........long length....G0000101  ..........char *chars...........
  830. msymbol .........long length....G0000111  ..........char *chars...........
  831. string  .........long length....G0001101  ..........char *chars...........
  832. vector  .........long length....G0001111  ...........SCM **elts...........
  833. bvect   .........long length....G0010101  ..........long *words...........
  834.  spare                          G0010111
  835. ivect   .........long length....G0011101  ..........long *words...........
  836. uvect   .........long length....G0011111  ......unsigned long *words......
  837.  spare                          G0100101
  838.  spare                          G0100111
  839. fvect   .........long length....G0101101  .........float *words...........
  840. dvect   .........long length....G0101111  ........double *words...........
  841. cvect   .........long length....G0110101  ........double *words...........
  842.  
  843. contin  .........long length....G0111101  .............*regs..............
  844. cclo    .........long length....G0111111  ...........SCM **elts...........
  845.         SUBRs:
  846.  spare                          010001x1
  847.  spare                          010011x1
  848. subr_0  ..........int hpoff.....01010101  ...........SCM (*f)()...........
  849. subr_1  ..........int hpoff.....01010111  ...........SCM (*f)()...........
  850. cxr     ..........int hpoff.....01011101  .........double (*f)()..........
  851. subr_3  ..........int hpoff.....01011111  ...........SCM (*f)()...........
  852. subr_2  ..........int hpoff.....01100101  ...........SCM (*f)()...........
  853. asubr   ..........int hpoff.....01100111  ...........SCM (*f)()...........
  854. subr_1o ..........int hpoff.....01101101  ...........SCM (*f)()...........
  855. subr_2o ..........int hpoff.....01101111  ...........SCM (*f)()...........
  856. lsubr_2 ..........int hpoff.....01110101  ...........SCM (*f)()...........
  857. lsubr_2n..........int hpoff.....01110111  ...........SCM (*f)()...........
  858. rpsubr  ..........int hpoff.....01111101  ...........SCM (*f)()...........
  859.                         PTOBs:
  860.    port            0bwroxxxxxxxxG1110111  ..........FILE *stream..........
  861.  socket ttttttt    00001xxxxxxxxG1110111  ..........FILE *stream..........
  862.  inport uuuuuuuuuuU00011xxxxxxxxG1110111  ..........FILE *stream..........
  863. outport 0000000000000101xxxxxxxxG1110111  ..........FILE *stream..........
  864.  ioport uuuuuuuuuuU00111xxxxxxxxG1110111  ..........FILE *stream..........
  865. fport              00   00000000G1110111  ..........FILE *stream..........
  866. pipe               00   00000001G1110111  ..........FILE *stream..........
  867. strport            00   00000010G1110111  ..........FILE *stream..........
  868. sfport             00   00000011G1110111  ..........FILE *stream..........
  869.                         SMOBs:
  870. free_cell
  871.         000000000000000000000000G1111111  ...........*free_cell........000
  872. flo     000000000000000000000001G1111111  ...........float num............
  873. dblr    000000000000000100000001G1111111  ..........double *real..........
  874. dblc    000000000000001100000001G1111111  .........complex *cmpx..........
  875. bignum  ...int length...0000001 G1111111  .........short *digits..........
  876. bigpos  ...int length...00000010G1111111  .........short *digits..........
  877. bigneg  ...int length...00000011G1111111  .........short *digits..........
  878.                         xxxxxxxx = code assigned by newsmob();
  879. promise 000000000000000fxxxxxxxxG1111111  ...........SCM val..............
  880. arbiter 000000000000000lxxxxxxxxG1111111  ...........SCM name.............
  881. macro   000000000000000mxxxxxxxxG1111111  ...........SCM name.............
  882. array   ...short rank..cxxxxxxxxG1111111  ............*array..............
  883.  
  884. 
  885. File: scm.info,  Node: Operations,  Next: Improvements To Make,  Prev: Data Types,  Up: Internals
  886.  
  887. Operations
  888. ==========
  889.  
  890. * Menu:
  891.  
  892. * Garbage Collection::          Automatically reclaims unused storage
  893. * Signals::
  894. * C Macros::
  895. * Changing Scm::                How to *really* screw it up
  896. * Defining Subrs::
  897. * Defining Smobs::
  898. * Defining Ptobs::
  899. * Calling Scheme From C::
  900. * Continuations::
  901. * Evaluation::                  Why SCM is fast
  902.  
  903. 
  904. File: scm.info,  Node: Garbage Collection,  Next: Signals,  Prev: Operations,  Up: Operations
  905.  
  906. Garbage Collection
  907. ------------------
  908.  
  909.   The garbage collector is in the latter half of `sys.c'.  Immediates
  910. always appear as parts of other objects, so they are not subject to
  911. explicit garbage collection.  There is a heap (composed of heap
  912. segments) in which all cells reside.  The storage for strings, vectors,
  913. continuations, doubles, complexes, and bignums is managed by malloc.
  914. There is only one pointer to each malloc object from its type-header
  915. cell in the heap.  This allows malloc objects to be freed when the
  916. associated heap object is garbage collected.
  917.  
  918.   To garbage collect, first certain protected objects are marked (such
  919. as symhash).  Then the stack (and marked continuations) are traversed.
  920. Each longword in the stack is tried to see if it is a valid cell pointer
  921. into the heap.  If it is, the object itself and any objects it points to
  922. are marked.  If the stack is word rather than longword aligned
  923. `(#define WORD_ALIGN)', both alignments are tried.  This arrangement
  924. will occasionally mark an object which is no longer used.  This has not
  925. been a problem in practice and the advantage of using the c-stack far
  926. outweighs it.
  927.  
  928.   The heap is then swept.  If a type-header cell pointing to malloc
  929. space is collected the malloc object is then freed.  If the type header
  930. of smob is collected, the smob's free procedure is called to free its
  931. storage.
  932.  
  933. 
  934. File: scm.info,  Node: Signals,  Next: C Macros,  Prev: Garbage Collection,  Up: Operations
  935.  
  936. Signals
  937. -------
  938.  
  939.  - Function: init_signals
  940.      (in `scm.c') initializes handlers for `SIGINT' and `SIGALRM' if
  941.      they are supported by the C implementation.  All of the signal
  942.      handlers immediately reestablish themselves by a call to
  943.      `signal()'.
  944.  
  945.  - Function: int_signal SIG
  946.  - Function: alrm_signal SIG
  947.      The low level handlers for `SIGINT' and `SIGALRM'.
  948.  
  949.   If an interrupt handler is defined when the interrupt is received, the
  950. code is interpreted.  If the code returns, execution resumes from where
  951. the interrupt happened.  `Call-with-current-continuation' allows the
  952. stack to be saved and restored.
  953.  
  954.   SCM does not use any signal masking system calls.  These are not a
  955. portable feature.  However, code can run uninterrupted by use of the C
  956. macros `DEFER_INTS' and `ALLOW_INTS'.
  957.  
  958.  - Macro: DEFER_INTS
  959.      sets the global variable `ints_disabled' to 1.  If an interrupt
  960.      occurs during a time when `ints_disabled' is 1 one of the global
  961.      variables `sig_deferred' or `alrm_deferred' is set to 1 and the
  962.      handler returns.
  963.  
  964.  - Macro: ALLOW_INTS
  965.      Checks the deferred variables and if set the appropriate handler is
  966.      called.
  967.  
  968.      Calls to `DEFER_INTS' can not be nested.  An `ALLOW_INTS' must
  969.      happen before another `DEFER_INTS' can be done.  In order to check
  970.      that this constraint is satisfied `#define CAREFUL_INTS' in
  971.      `scmfig.h'.
  972.  
  973. 
  974. File: scm.info,  Node: C Macros,  Next: Changing Scm,  Prev: Signals,  Up: Operations
  975.  
  976. C Macros
  977. --------
  978.  
  979.  - Macro: ASSERT COND ARG POS SUBR
  980.      signals an error if the expression (COND) is 0.  ARG is the
  981.      offending object, SUBR is the string naming the subr, and POS
  982.      indicates the position or type of error.  POS can be one of
  983.  
  984.         * `ARGn' (> 5 or unknown ARG number)
  985.  
  986.         * `ARG1'
  987.  
  988.         * `ARG2'
  989.  
  990.         * `ARG3'
  991.  
  992.         * `ARG4'
  993.  
  994.         * `ARG5'
  995.  
  996.         * `WNA' (wrong number of args)
  997.  
  998.         * `OVFLOW'
  999.  
  1000.         * `OUTOFRANGE'
  1001.  
  1002.         * `NALLOC'
  1003.  
  1004.         * `EXIT'
  1005.  
  1006.         * `HUP_SIGNAL'
  1007.  
  1008.         * `INT_SIGNAL'
  1009.  
  1010.         * `FPE_SIGNAL'
  1011.  
  1012.         * `BUS_SIGNAL'
  1013.  
  1014.         * `SEGV_SIGNAL'
  1015.  
  1016.         * `ALRM_SIGNAL'
  1017.  
  1018.         * a C string `(char *)'
  1019.  
  1020.      Error checking is not done by `ASSERT' if the flag `RECKLESS' is
  1021.      defined.  An error condition can still be signaled in this case
  1022.      with a call to `wta(arg, pos, subr)'.
  1023.  
  1024.  - Macro: ASRTGO COND LABEL
  1025.      `goto' LABEL if the expression (COND) is 0.  Like `ASSERT',
  1026.      `ASRTGO' does is not active if the flag `RECKLESS' is defined.
  1027.  
  1028. 
  1029. File: scm.info,  Node: Changing Scm,  Next: Defining Subrs,  Prev: C Macros,  Up: Operations
  1030.  
  1031. Changing Scm
  1032. ------------
  1033.  
  1034. When writing C-code for SCM, a precaution is recommended.  If your
  1035. routine allocates a non-cons cell which will *not* be incorporated into
  1036. a `SCM' object which is returned, you need to make sure that a `SCM'
  1037. variable in your routine points to that cell as long as part of it
  1038. might be referenced by your code.
  1039.  
  1040. In order to make sure this `SCM' variable does not get optimized out
  1041. you can put this assignment after its last possible use:
  1042.  
  1043.      SCM_dummy1 = foo;
  1044.  
  1045. or put this assignment somewhere in your routine:
  1046.  
  1047.      SCM_dummy1 = (SCM) &foo;
  1048.  
  1049. `SCM_dummy' variables are not currently defined.  Passing the address
  1050. of the local `SCM' variable to *any* procedure also protects it.
  1051.  
  1052. Also, if you maintain a static pointer to some (non-immediate) `SCM'
  1053. object, you must either make your pointer be the value cell of a symbol
  1054. (see `errobj' for an example) or make your pointer be one of the
  1055. `sys_protects' (see `symhash' for an example).  The former method is
  1056. prefered since it does not require any changes to the SCM distribution.
  1057.  
  1058. To add a C routine to scm:
  1059.  
  1060.   1. choose the appropriate subr type from the type list.
  1061.  
  1062.   2. write the code and put into `scm.c'.
  1063.  
  1064.   3. add a `make_subr' or `make_gsubr' call to `init_scm'.  Or put an
  1065.      entry into the appropriate `iproc' structure.
  1066.  
  1067.   To add a package of new procedures to scm (see `crs.c' for example):
  1068.  
  1069.   1. create a new C file (`foo.c').
  1070.  
  1071.   2. at the front of `foo.c' put declarations for strings for your
  1072.      procedure names.
  1073.  
  1074.           static char s_twiddle_bits[]="twiddle-bits!";
  1075.           static char s_bitsp[]="bits?";
  1076.  
  1077.   3. choose the appropriate subr types from the type list in `code.doc'.
  1078.  
  1079.   4. write the code for the procedures and put into `foo.c'
  1080.  
  1081.   5. create one `iproc' structure for each subr type used in `foo.c'
  1082.  
  1083.           static iproc subr3s[]= {
  1084.                   {s_twiddle-bits,twiddle-bits},
  1085.                   {s_bitsp,bitsp},
  1086.                   {0,0} };
  1087.  
  1088.   6. create an `init_<name of file>' routine at the end of the file
  1089.      which calls `init_iprocs' with the correct type for each of the
  1090.      `iproc's created in step 5.
  1091.  
  1092.           void init_foo()
  1093.           {
  1094.             init_iprocs(subr1s, tc7_subr_1);
  1095.             init_iprocs(subr3s, tc7_subr_3);
  1096.           }
  1097.  
  1098.      If your package needs to have a "finalization" routine called to
  1099.      free up storage, close files, etc, then also have a line in
  1100.      `init_foo' like:
  1101.  
  1102.           add_final(final_foo);
  1103.  
  1104.      `final_foo' should be a (void) procedure of no arguments.  The
  1105.      finals will be called in opposite order from their definition.
  1106.  
  1107.      The line:
  1108.  
  1109.           add_feature("foo");
  1110.  
  1111.      will append a symbol `'foo' to the (list) value of `*features*'.
  1112.  
  1113.   7. put any scheme code which needs to be run as part of your package
  1114.      into `Ifoo.scm'.
  1115.  
  1116.   8. put an `if' into `Init.scm' which loads `Ifoo.scm' if your package
  1117.      is included:
  1118.  
  1119.           (if (defined? twiddle-bits!)
  1120.               (load (in-vicinity (implementation-vicinity)
  1121.                                  "Ifoo"
  1122.                                  (scheme-file-suffix))))
  1123.  
  1124.      or use `(provided? 'foo)' instead of `(defined?  twiddle-bits!)'
  1125.      if you have added the feature.
  1126.  
  1127.   9. put documentation of the new procedures into `foo.doc'
  1128.  
  1129.  10. add lines to your `Makefile' to compile and link SCM with your
  1130.      object file.  Add a `init_foo\(\)\;' to the `INITS=...' line at
  1131.      the beginning of the makefile.
  1132.  
  1133. These steps should allow your package to be linked into SCM with a
  1134. minimum of difficulty.  Your package should also work with dynamic
  1135. linking if your SCM has this capability.
  1136.  
  1137.   Special forms (new syntax) can be added to scm.
  1138.  
  1139.   1. define a new `MAKISYM' in `scm.h' and increment `NUM_ISYMS'.
  1140.  
  1141.   2. add a string with the new name in the corresponding place in
  1142.      `isymnames' in `repl.c'.
  1143.  
  1144.   3. add `case:' clause to `ceval()' near `i_quasiquote' (in `eval.c').
  1145.  
  1146. New syntax can now be added without recompiling SCM by the use of the
  1147. `procedure->syntax', `procedure->macro', `procedure->memoizing-macro',
  1148. and `defmacro'.  For details, *Note Syntax Extensions::.
  1149.  
  1150. 
  1151. File: scm.info,  Node: Defining Subrs,  Next: Defining Smobs,  Prev: Changing Scm,  Up: Operations
  1152.  
  1153. Defining Subrs
  1154. --------------
  1155.  
  1156. If "CCLO" is `#define'd when compiling, the compiled closure feature
  1157. will be enabled.  It is automatically enabled if dynamic linking is
  1158. enabled.
  1159.  
  1160. The SCM interpreter directly recognizes subrs taking small numbers of
  1161. arguments.  In order to create subrs taking larger numbers of arguments
  1162. use:
  1163.  
  1164.  - Function: make_gsubr NAME REQ OPT REST FCN
  1165.      returns a cclo (compiled closure) object of name `char *' NAME
  1166.      which takes `int' REQ required arguments, `int' OPT optional
  1167.      arguments, and a list of rest arguments if `int' REST is 1 (0 for
  1168.      not).
  1169.  
  1170.      `SCM (*fcn)()' is a pointer to a C function to do the work.
  1171.  
  1172.      The C function will always be called with REQ + OPT + REST
  1173.      arguments, optional arguments not supplied will be passed
  1174.      `UNDEFINED'.  An error will be signaled if the subr is called with
  1175.      too many or too few arguments.  Currently a total of 10 arguments
  1176.      may be specified, but increasing this limit should not be
  1177.      difficult.
  1178.  
  1179.           /* A silly example, taking 2 required args,
  1180.              1 optional, and a list of rest args */
  1181.           
  1182.           SCM gsubr_21l(req1,req2,opt,rst)
  1183.                SCM req1,req2,opt,rst;
  1184.           {
  1185.             lputs("gsubr-2-1-l:\n req1: ", cur_outp);
  1186.             display(req1,cur_outp);
  1187.             lputs("\n req2: ", cur_outp);
  1188.             display(req2,cur_outp);
  1189.             lputs("\n opt: ", cur_outp);
  1190.             display(opt,cur_outp);
  1191.             lputs("\n rest: ", cur_outp);
  1192.             display(rst,cur_outp);
  1193.             newline(cur_outp);
  1194.             return UNSPECIFIED;
  1195.           }
  1196.           
  1197.           void init_gsubr211()
  1198.           {
  1199.             make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l);
  1200.           }
  1201.  
  1202. 
  1203. File: scm.info,  Node: Defining Smobs,  Next: Defining Ptobs,  Prev: Defining Subrs,  Up: Operations
  1204.  
  1205. Defining Smobs
  1206. --------------
  1207.  
  1208. Here is an example of how to add a new type named `foo' to SCM.  The
  1209. following lines need to be added to your code:
  1210.  
  1211. `long tc16_foo;'
  1212.      The type code which will be used to identify the new type.
  1213.  
  1214. `static smobfuns foosmob = {markfoo,freefoo,printfoo,equalpfoo};'
  1215.      smobfuns is a structure composed of 4 functions:
  1216.  
  1217.           typedef struct {
  1218.             SCM   (*mark)P((SCM));
  1219.             sizet (*free)P((CELLPTR));
  1220.             int   (*print)P((SCM exp, SCM port, int writing));
  1221.             SCM   (*equalp)P((SCM, SCM));
  1222.           } smobfuns;
  1223.  
  1224.     `smob.mark'
  1225.           is a function of one argument of type `SCM' (the cell to
  1226.           mark) and returns type `SCM' which will then be marked.  If
  1227.           no further objects need to be marked then return an immediate
  1228.           object such as `BOOL_F'.  2 functions are provided:
  1229.  
  1230.          `markcdr(ptr)'
  1231.                which marks the current cell and returns `CDR(ptr)'.
  1232.  
  1233.          `mark0(ptr)'
  1234.                which marks the current cell and returns `BOOL_F'.
  1235.  
  1236.     `smob.free'
  1237.           is a function of one argument of type `CELLPTR' (the cell to
  1238.           collected) and returns type `sizet' which is the number of
  1239.           `malloc'ed bytes which were freed.  `Smob.free' should free
  1240.           any `malloc'ed storage associated with this object.  The
  1241.           function free0(ptr) is provided which does not free any
  1242.           storage and returns 0.
  1243.  
  1244.     `smob.print'
  1245.           is 0 or a function of 3 arguments.  The first, of type `SCM',
  1246.           is the smob object.  The second, of type `SCM', is the stream
  1247.           on which to write the result.  The third, of type int, is 1
  1248.           if the object should be `write'n, 0 if it should be
  1249.           `display'ed.  This function should return non-zero if it
  1250.           printed, and zero otherwise (in which case a hexadecimal
  1251.           number will be printed).
  1252.  
  1253.     `smob.equalp'
  1254.           is 0 or a function of 2 `SCM' arguments.  Both of these
  1255.           arguments will be of type `tc16foo'.  This function should
  1256.           return `BOOL_T' if the smobs are equal, `BOOL_F' if they are
  1257.           not.  If `smob.equalp' is 0, `equal?' will return `BOOL_F' if
  1258.           they are not `eq?'.
  1259.  
  1260. `tc16_foo = newsmob(&foosmob);'
  1261.      Allocates the new type with the functions from `foosmob'.  This
  1262.      line goes in an `init_' routine.
  1263.  
  1264. Promises and macros in `eval.c' and arbiters in `repl.c' provide
  1265. examples of SMOBs.  There are a maximum of 256 SMOBs.
  1266.  
  1267. 
  1268. File: scm.info,  Node: Defining Ptobs,  Next: Calling Scheme From C,  Prev: Defining Smobs,  Up: Operations
  1269.  
  1270. Defining Ptobs
  1271. --------------
  1272.  
  1273. "ptob"s are similar to smobs but define new types of port to which SCM
  1274. procedures can read or write.  The following functions are defined in
  1275. the `ptobfuns':
  1276.  
  1277.      typedef struct {
  1278.        SCM   (*mark)P((SCM ptr));
  1279.        int   (*free)P((FILE *p));
  1280.        int   (*print)P((SCM exp, SCM port, int writing));
  1281.        SCM   (*equalp)P((SCM, SCM));
  1282.        int   (*fputc)P((int c, FILE *p));
  1283.        int   (*fputs)P((char *s, FILE *p));
  1284.        sizet (*fwrite)P((char *s, sizet siz, sizet num, FILE *p));
  1285.        int   (*fflush)P((FILE *stream));
  1286.        int   (*fgetc)P((FILE *p));
  1287.        int   (*fclose)P((FILE *p));
  1288.      } ptobfuns;
  1289.  
  1290. The `.free' component to the structure takes a `FILE *' or other C
  1291. construct as its argument, unlike `.free' in a smob, which takes the
  1292. whole smob cell.  Often, `.free' and `.fclose' can be the same
  1293. function.  See `fptob' and `pipob' in `sys.c' for examples of how to
  1294. define ptobs.
  1295.  
  1296. 
  1297. File: scm.info,  Node: Calling Scheme From C,  Next: Continuations,  Prev: Defining Ptobs,  Up: Operations
  1298.  
  1299. Calling Scheme From C
  1300. ---------------------
  1301.  
  1302. To use SCM as a whole from another program call `init_scm' or `run_scm'
  1303. as is done in `main()' in `scm.c'.
  1304.  
  1305. In order to call indivdual Scheme procedures from C code more is
  1306. required; SCM's storage system needs to be initialized.  The simplest
  1307. way to do this for a statically linked single-thread program is to:
  1308.  
  1309.   1. make a SCM procedure which calls your code's startup routine.
  1310.  
  1311.   2. use the `#define RTL' flag when compiling `scm.c' to elide SCM's
  1312.      `main()'.
  1313.  
  1314.   3. In your `main()', call `run_scm' with arguments (`argc' and
  1315.      `argv') to invoke your code's startup routine.
  1316.  
  1317.   4. link your code with SCM at compile time.
  1318.  
  1319. For a dynamically linked single-thread program:
  1320.  
  1321.   1. make an `init_' procedure for your code which will set up any
  1322.      Scheme definitions you need and then call your startup routine
  1323.      (*note Changing Scm::.).
  1324.  
  1325.   2. Start SCM with command line arguments to dynamically link your
  1326.      code.  After your module is linked, the `init_' procedure will be
  1327.      called, and hence your startup routine.
  1328.  
  1329. Now use `apply' (and perhaps `intern') to call Scheme procedures from
  1330. your C code.  For example:
  1331.  
  1332.      /* If this apply fails, SCM will catch the error */
  1333.      apply(CDR(intern("srv:startup",sizeof("srv:startup")-1)),
  1334.            mksproc(srvproc),
  1335.            listofnull);
  1336.      
  1337.      func = CDR(intern(rpcname,strlen(rpcname)));
  1338.      retval = apply(func, cons(mksproc(srvproc), args), EOL);
  1339.  
  1340. SCM now has routines to make calling back to Scheme procedures easier:
  1341.  
  1342.  - Function: int scm_ldfile (char *FILE)
  1343.      Loads the Scheme source file FILE.  Returns 0 if successful, non-0
  1344.      if not.  This function is used to load SCM's initialization file
  1345.      `Init.scm'.
  1346.  
  1347.  - Function: int scm_ldprog (char *FILE)
  1348.      Loads the Scheme source file `(in-vicinity (program-vicinity)
  1349.      FILE)'.  Returns 0 if successful, non-0 if not.
  1350.  
  1351.      This function is useful for compiled code init_ functions to load
  1352.      non-compiled Scheme (source) files.  `program-vicinity' is the
  1353.      directory from which the calling code was loaded (*note Vicinity:
  1354.      (slib)Vicinity.).
  1355.  
  1356.  - Function: SCM scm_evstr (char *STR)
  1357.      Returns the result of reading an expression from STR and
  1358.      evaluating it.
  1359.  
  1360.  - Function: void scm_ldstr (char *STR)
  1361.      Reads and evaluates all the expressions from STR.
  1362.  
  1363. If you wish to catch errors during execution of Scheme code, then you
  1364. can use a wrapper like this for your Scheme procedures:
  1365.  
  1366.      (define (srv:protect proc)
  1367.        (lambda args
  1368.          (define result #f)                  ; put default value here
  1369.          (call-with-current-continuation
  1370.           (lambda (cont)
  1371.             (dynamic-wind (lambda () #t)
  1372.                           (lambda ()
  1373.                             (set! result (apply proc args))
  1374.                             (set! cont #f))
  1375.                           (lambda ()
  1376.                             (if cont (cont #f))))))
  1377.          result))
  1378.  
  1379. Calls to procedures so wrapped will return even if an error occurs.
  1380.  
  1381. 
  1382. File: scm.info,  Node: Continuations,  Next: Evaluation,  Prev: Calling Scheme From C,  Up: Operations
  1383.  
  1384. Continuations
  1385. -------------
  1386.  
  1387. The scm procedure call-with-current-continuation calls it's argument
  1388. with an object of type `contin'.
  1389.  
  1390. If `CHEAP_CONTINUATIONS' is #defined (in `scmfig.h') the contin just
  1391. contains a jmp_buf.  When the contin is applied, a longjmp of the
  1392. jmp_buf is done.
  1393.  
  1394. If `CHEAP_CONTINUATIONS' is not #defined the contin contains the
  1395. jmp_buf and a copy of the C stack between the call_cc stack frame and
  1396. BASE(rootcont).  When the contin is applied:
  1397.  
  1398.    * the stack is grown larger than the saved stack, if neccessary.
  1399.  
  1400.    * the saved stack is copied back into it's original position.
  1401.  
  1402.    * longjmp of the jmp_buf is called.
  1403.  
  1404. On systems with nonlinear stack disciplines (multiple stacks or
  1405. non-contiguous stack frames) copying the stack will not work properly.
  1406. These systems need to #define `CHEAP_CONTINUATIONS' in `scmfig.h'.
  1407.  
  1408.